import 'package:gold/base/view_model.dart';
import 'package:gold/http/api_gold.dart';
import 'package:gold/http/http.dart';
import 'package:gold/model/gold_banner_model.dart';
import 'package:gold/model/goods_detail_model.dart';
import 'package:gold/model/goods_list_model.dart';
import 'package:gold/model/list_model.dart';
import 'package:gold/route/routes.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/utils/xfg_extension.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

/// @description:
/// @author tangzhentao
/// @created at 19:22 2024/11/30


class GoldInvestViewmodel extends Viewmodel {
  /// 刷新控制器
  final RefreshController controller = RefreshController();

  /// 轮播图
  List<GoldBannerModel> banners = [];

  /// 商品列表
  final List<GoodsListModel> goodsList = [];

  /// 页码
  int _page = 1;

  /// 加载数据
  Future load() async {
    final bannerResult = await Http().requestList<GoldBannerModel>(ApiGold.investBanner, showErrorMsg: false);
    banners = bannerResult.data?? [];
    final result = await Http().request<ListModel<GoodsListModel>>(ApiGold.goldList,
        data: {'page': _page, 'filter': {'keywords': ''}, 'sort': {'sales': 0, 'price': 0}}
    );
    goodsList.addAll(result.data?.list?? []);
    setSuccessChange();
    controller.headerMode?.value = RefreshStatus.canRefresh;
    controller.footerMode?.value = _page < (result.data?.lastPage?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
  }

  /// 刷新数据
  Future refresh() async {
    _page = 1;
    final result = await Http().request<ListModel<GoodsListModel>>(ApiGold.goldList,
        data: {'page': _page, 'filter': {'keywords': ''}, 'sort': {'sales': 0, 'price': 0}}
    );
    if (result.success) {
      controller.refreshCompleted();
      controller.footerMode?.value = _page < (result.data?.lastPage?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      final data = result.data?.list?? [];
      if (data.isNotEmpty) {
        goodsList.clear();
        goodsList.addAll(data);
        setSuccessChange();
      }
    } else {
      controller.refreshFailed();
    }
  }

  /// 加载更多数据
  Future loadMore() async {
    _page++;
    final result = await Http().request<ListModel<GoodsListModel>>(ApiGold.goldList,
        data: {'page': _page, 'filter': {'keywords': ''}, 'sort': {'sales': 0, 'price': 0}}
    );
    if (result.success) {
      controller.loadComplete();
      controller.footerMode?.value = _page < (result.data?.lastPage?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      final data = result.data?.list?? [];
      if (data.isNotEmpty) {
        goodsList.addAll(data);
        setSuccessChange();
      } else {
        controller.loadNoData();
      }
    } else {
      controller.loadFailed();
    }
  }

  /// 去黄金投资详情页面
  void toGoldInvestDetailPage(int index) {
    final id = goodsList[index].id;
    if (id != null) {
      XfgNav.pushNamed(Routes.goldInvestDetail, arguments: id);
    } else {
      '商品信息缺失'.toast();
    }
  }

  /// 去黄金投资订单页面
  Future toGoodsOrderPage(int index) async {
    final result = await Http().request<GoodsDetailModel>(ApiGold.goldDetail, data: {'id': goodsList[index].id}, loadingText: '加载中...');
    if (result.success) {
      if (result.data != null) {
        XfgNav.pushNamed(Routes.goldInvestDetailOrder, arguments: result.data!);
      } else {
        '商品信息异常'.toast();
      }
    }
  }
}